package com.campus.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.activity.entity.Student;
import com.campus.activity.mapper.StudentMapper;
import com.campus.activity.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 学生服务实现类
 * 
 * @author System
 * @since 2024
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public boolean addStudent(Student student) {
        // 检查学号是否已存在
        if (isStudentNumberExists(student.getStudentNumber())) {
            return false;
        }
        
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setDeleted(0);
        return save(student);
    }

    @Override
    public boolean updateStudent(Integer studentId, Student student) {
        student.setStudentId(studentId);
        student.setUpdateTime(LocalDateTime.now());
        return updateById(student);
    }

    @Override
    public boolean deleteStudent(Integer studentId) {
        Student student = new Student();
        student.setStudentId(studentId);
        student.setDeleted(1);
        student.setUpdateTime(LocalDateTime.now());
        return updateById(student);
    }

    @Override
    public Student getStudentByNumber(String studentNumber) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_number", studentNumber)
                   .eq("deleted", 0);
        return getOne(queryWrapper);
    }

    @Override
    public List<Student> getStudentsByName(String studentName) {
        return studentMapper.selectByStudentName(studentName);
    }

    @Override
    public boolean isStudentNumberExists(String studentNumber) {
        return getStudentByNumber(studentNumber) != null;
    }

    @Override
    public List<Student> getAllStudents() {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0)
                   .orderByAsc("student_number");
        return list(queryWrapper);
    }
}